package com.lanhe.app.controller.qingchuang;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSON;
import com.lanhe.apply.domain.LhQingchuangApply;
import com.lanhe.apply.service.ILhQingchuangApplyService;
import com.lanhe.common.core.controller.BaseController;
import com.lanhe.common.core.domain.AjaxResult;
import com.lanhe.common.core.page.TableDataInfo;
import com.lanhe.common.utils.StringUtils;
import com.lanhe.common.utils.qingchuang.dto.QingChuangApplyDTO;
import com.lanhe.apply.domain.dto.LhOrdersQingChuangApplyDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhanggl
 * @date 2023/12/15
 */
@RestController
@RequestMapping("/api/qingchuang/apply")
@Slf4j
public class ApiQingChuangApplyController extends BaseController {

	@Autowired
	private ILhQingchuangApplyService applyService;

	/**
	 * 报备数据列表
	 *
	 * @param pid      产品ID
	 * @param pageNum  页码
	 * @param pageSize 页数
	 * @author zhanggl
	 * @date 2023/12/18
	 */
/*	@GetMapping("/list")
	public AjaxResult list(@RequestParam(name = "pid") Long pid,
	                       @RequestParam(name = "status", defaultValue = "") Long status,
	                       @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
	                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) throws Exception {

		String resource = applyService.applyList(getLoginUser().getUserKey(), pid, status, pageNum, pageSize);
		QingChuangApplyListVO applyList = JSONObject.parseObject(resource, QingChuangApplyListVO.class);
		if (!Constants.GeneralIntType.ONE.equals(applyList.getCode())) {
			throw new ServiceException("报备列表异常->" + applyList.getMsg(), HttpStatus.ERROR);
		}
		return success(applyList);
	}*/

	/**
	 * 分页查询所有数据
	 */
	@GetMapping("/list")
	public TableDataInfo list(LhQingchuangApply apply) {
		startPage();
		apply.setMemberId(getUserId());
		List<LhQingchuangApply> list = applyService.selectApplyList(apply);
		return getDataTable(list);
	}

	/**
	 * 新增报备数据
	 *
	 * @param applyDto 实体对象
	 * @return 新增结果
	 */
	@PostMapping("/add")
	public AjaxResult add(@RequestBody QingChuangApplyDTO applyDto) throws Exception {

		if (StringUtils.isBlank(applyDto.getNumber())) {
			return error("缺少必须参数，产品编号不能为空");
		}
		if (ObjectUtil.isEmpty(applyDto.getText())) {
			return error("缺少必须参数，提交表单内容不能为空");
		}
		applyDto.setUid(getLoginUser().getUserKey());
		applyDto.setMemberId(getUserId());
		applyDto.setMobile(getLoginUser().getLhMemberBase().getMobile());
		String applyId = applyService.insertApply(applyDto);
		Map<String, String> map = new HashMap<>();
		map.put("data", applyId);
		return success(map);
	}


	/**
	 * 报备数据回调
	 *
	 * @param qingChuangApplyDto 实体对象
	 * @return 结果
	 */
	@PostMapping("/applyNotice")
	public Object applyNotice(LhOrdersQingChuangApplyDto qingChuangApplyDto) throws Exception {
		log.info("ApiQingChuangApplyController--->applyNotice 清创：【{}】", JSON.toJSONString(qingChuangApplyDto));
		if (ObjectUtil.isNull(qingChuangApplyDto)) {
			return toAjaxDuoyou(200);
		}
		applyService.applyNotice(qingChuangApplyDto);
		return toAjaxDuoyou(200);
	}
}

